package com.topvision.myviewtest.mvp.demo3.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/**
 * @author lucy
 * @date 2017-12-11 17:16
 * @description 1.指定子类具体的View必须继承自IMvpBaseView4
 * 2.指定子类具体的Presenter必须继承自AbstractMvpPersenter4
 */

public abstract class AbstractMvpActivity<V extends IMvpBaseView3, P extends AbstractMvpPresenter3>
        extends AppCompatActivity implements IMvpBaseView3 {
    private P mP;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 创建presenter
        if (mP == null) {
            mP = createPresenter();
        }

        if (mP == null) {
            throw new NullPointerException("presenter 不能为空");
        }

        // 绑定View
        mP.attachMvpView((V) this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mP != null) {
            mP.detachMvpView();
        }
    }

    public P getPresenter() {
        return mP;
    }

    protected abstract P createPresenter();
}
